<template>
    <div>
        商品管理登录
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model.number="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {ElMessage} from "element-plus";

    export default {
        name: "Login",
        data() {

            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                }
                callback();
            };
            var validateUsername = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                }
                callback();
            };
            return {
                ruleForm: {
                    password: '',
                    username: ''
                },
                rules: {
                    password: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    username: [
                        { validator: validateUsername, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('http://localhost:8080//users/ums-admin/login', this.ruleForm)
                            .then(res=>{
                                if(res.data.status == 200){
                                    ElMessage.success({
                                        message: '登录成功。',
                                        type: 'success'
                                    });
                                    debugger
                                    //jwt存放
                                    window.sessionStorage.setItem("token",res.data.token)
                                    this.$store.commit('initUserInfo')
                                    //解析的名字存放
                                    this.$router.push("/main")
                                }else{
                                    window.sessionStorage.removeItem("token")
                                }

                            })
                            .catch(function (error) {
                                debugger
                                console.log(error);
                            });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>
